import GlobalService from '../global/GlobalService'
import store         from '@/store/store.js'

let instance = null

// 后端路径
const prefix = '/info'

class Service extends GlobalService {
	constructor() {
		super()
		return instance || this
	}
	
	
	static getInstance() {
		if (!instance) {
			instance = new Service()
		}
		return instance
	}
	
	/**
	 * 获取要件列配置
	 * @param itemId
	 * @returns {Promise<*>}
	 */
	getItemColumns = async (itemId) => {
		return this.get(`${ prefix }/searchItemColList?itemId=${ itemId }`)
	}
	
	/**
	 * 获取要件列配置
	 * @param prjId
	 * @param taskId
	 * @param workId
	 * @param itemId
	 * @returns {Promise<*>}
	 */
	getList = async ({ prjId, taskId, workId, itemId }) => {
		const query = this.qs({ prjId, taskId, workId, itemId })
		const ret = await this.get(`${ prefix }/searchItemInfo?${ query }`) || []
		return ret.map(e => e.itemInfo)
	}
	
	
	/**
	 * 新增
	 * @param model
	 * @returns {Promise<*>}
	 */
	addItem = async (model) => {
		return this.post(`${ prefix }/saveItemInfo`, model)
	}
	
	/**
	 * 编辑
	 * @param model object
	 * @returns {Promise<*>}
	 */
	editItem = async (model) => {
		return this.post(`${ prefix }/updateItemInfo`, model)
	}
	
	/**
	 * 删除
	 * @param prjId
	 * @param infoId
	 * @returns {Promise<*>}
	 */
	delItem = async (prjId, infoId) => {
		const query = this.qs({ prjId, infoId, })
		return this.delete(`${ prefix }/delItemInfo?${ query }`)
	}
	
	/**
	 * 获取项目进度树
	 * @returns {Promise<*>}
	 */
	getTreeData = async (prjid) => {
		const { subList = [] } = await this.get(`${ prefix }/todoList?prjid=${ prjid }`) || {};
		store.commit('setSelectedTask', null)
		return subList
	}
	
	/**
	 * 获取 任务下的所有要件
	 * @param taskid
	 * @returns {Promise<*>}
	 */
	getItemList = async (taskid) => {
		store.commit('setState', { disableTaskBtn: false });//请求前先初始化一下按钮状态
		if (!taskid) return [];
		return this.get(`${ prefix }/searchItemList?taskid=${ taskid }`)
	}
	
	/**
	 * 保存要件信息
	 * @param model
	 * @returns {Promise<*>}
	 */
	saveItemList = async (model) => {
		return this.post(`${ prefix }/saveItemInfo`, model)
	}
	
	//提交任务
	commitTask = async (model) => {
		const query = this.qs(model)
		return this.post(`${ prefix }/commitTask?${ query }`, model)
	}
	
	//历史审批记录 单个任务
	querySpHis_byTask = (model) => {
		const query = this.qs(model)
		return this.get(`/rptSp/querySpHis_byTask?${ query }`)
	}
	
	//修改要件替换说明
	repleaseItemInfo = (model) => {
		const query = this.qs(model)
		return this.post(`${ prefix }/repleaseItemInfo?${ query }`, model)
	}
	
	//领导添加督办信息（鱼骨图专用）
	insertDb_fish = (model) => {
		const query = this.qs(model)
		return this.post(`/rptManTip/insertDb_fish?${ query }`, model)
	}
	
	//taskPzId
	queryOvertimeMessageList = (model) => {
		const query = this.qs(model)
		return this.get(`/taskWarnManagement/queryOvertimeMessageList?${ query }`)
	}
}

export default Service.getInstance()
